/**
* Tero Paananen
* tepaanan@gmail.com
* FINLAND
*/

#include <QtGui>
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDebug>

// Lock Symbian orientation
#ifdef Q_OS_SYMBIAN
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif

#ifdef Q_OS_SYMBIAN
#include <QSymbianEvent>
#include <w32std.h>
#endif

#include "cameraitem.h"
#include "serversocket.h"



// TODO: Where exists these values?
static const int KGoomMemoryLowEvent = 0x10282DBF;
static const int KGoomMemoryGoodEvent = 0x20026790;

/* ***************************************************************************
* MyApplication
*/
class MyApplication : public QApplication
{
public:
    MyApplication( int argc, char** argv ) : QApplication( argc, argv ) {}

#ifdef Q_OS_SYMBIAN
protected:
    bool symbianEventFilter( const QSymbianEvent* symbianEvent ) {
        const TWsEvent *event = symbianEvent->windowServerEvent();
        if( !event ) {
            return false;
        }
        switch( event->Type() ) {
        // GOOM handling enabled
        // http://wiki.forum.nokia.com/index.php/Graphics_memory_handling
        case EEventUser: {
            TApaSystemEvent* eventData = reinterpret_cast<TApaSystemEvent*>(event->EventData());
            if ((*eventData) == EApaSystemEventShutdown) {
                qDebug() << "EApaSystemEventShutdown";
                eventData++;
                if ((*eventData) == KGoomMemoryLowEvent) {
                    qDebug() << "KGoomMemoryLowEvent";
                    return true;
                }
            }
            break;
        }
        default:
            break;
        };

        // Always return false so we don't stop
        // the event from being processed
        return false;
    }
#endif
};




int main(int argc, char *argv[])
{
    MyApplication a(argc, argv);

    qmlRegisterType<CameraItem>("CustomItems", 1, 0, "CameraItem");
    qmlRegisterType<ServerSocket>("SocketItems", 1, 0, "ServerSocket");

    // Lock Symbian orientation
#ifdef Q_OS_SYMBIAN
    CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
    TRAP_IGNORE(
                if (appUi) {
                appUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
}
);
#endif

    QDeclarativeView* view = new QDeclarativeView();
    view->setAutoFillBackground(false);
    view->setSource(QUrl::fromLocalFile("qml/main.qml"));
    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view->showFullScreen();

    int ret = a.exec();
    delete view;
    return ret;
}
